package com.tulingxueyuan.mall.modules.pms.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tulingxueyuan.mall.common.api.CommonPage;
import com.tulingxueyuan.mall.common.api.CommonResult;
import com.tulingxueyuan.mall.modules.pms.model.PmsProductCategory;
import com.tulingxueyuan.mall.modules.pms.model.dto.PmsProductCategoryDTO;
import com.tulingxueyuan.mall.modules.pms.model.dto.ProductCateChildernDTO;
import com.tulingxueyuan.mall.modules.pms.service.PmsProductCategoryService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 * 产品分类 前端控制器
 * </p>
 *
 * @author zhaofuhao
 * @since 2022-02-24
 */
@RestController
@RequestMapping("/productCategory")
public class PmsProductCategoryController {
   @Autowired
   private PmsProductCategoryService pmsProductCategoryService;

    /*
    url:'/productCategory/list/'+parentId,
    method:'get',
    params:params
    */
    @GetMapping(value = "/list/{parentId}")
    public CommonResult<CommonPage<PmsProductCategory>> getList(@PathVariable long parentId,
                                                              @RequestParam(value = "pageNum",defaultValue  ="1")  Integer pageNum,
                                                                @RequestParam(value = "pageSize",defaultValue  ="5")  Integer pageSize){

       Page page= pmsProductCategoryService.list(parentId,pageNum,pageSize);

        return CommonResult.success(CommonPage.restPage(page));
    }
    @ApiOperation("修改导航栏状态")
    @PostMapping(value = "/update/navStatus")
    public CommonResult updateNavStatus(@RequestParam(value = "ids") List<Long> ids,
                                        @RequestParam(value = "navStatus")  Integer navStatus){


        boolean result  = pmsProductCategoryService.updateNavStatus(ids, navStatus);
        if (result){
            return CommonResult.success(result);
        }
        return CommonResult.failed();


    }
    @ApiOperation("修改显示状态")
    @PostMapping (value = "/update/showStatus")
    @ResponseBody
    public CommonResult updateShowStatus(@RequestParam("ids") List<Long> ids, @RequestParam("showStatus") Integer showStatus) {
        boolean count = pmsProductCategoryService.updateShowStatus(ids, showStatus);
        if (count) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }
   /*
   商品分类的删除
   /productCategory/delete/'+id
   * */
   @ApiOperation("商品分类的删除")
   @PostMapping (value = "/delete/{id}")
   @ResponseBody
   public CommonResult delete(@PathVariable("id") long id) {
       boolean count = pmsProductCategoryService.removeById(id);
       if (count) {
           return CommonResult.success(count);
       } else {
           return CommonResult.failed();
       }
   }
    /**
     * /productCategory/create
     * 商品分类添加
     */
    @ApiOperation("商品分类添加")
    @PostMapping (value = "/create")
    @ResponseBody
    public CommonResult create(@RequestBody PmsProductCategoryDTO productCategoryDTO) {
        boolean count = pmsProductCategoryService.customSave(productCategoryDTO);
        if (count) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }
    /**
     * 根据id获取商品分类
     *   return request({
     *     url:'/productCategory/'+id,
     *     method:'get',
     *   })
     */
    @ApiOperation("根据id查询商品单条记录")
    @GetMapping (value = "/{id}")
    @ResponseBody
    public CommonResult<PmsProductCategory> getById(@PathVariable long id) {
        PmsProductCategory productCategory = pmsProductCategoryService.getById(id);

        return CommonResult.success(productCategory);
    }
    /**
     *   修改
     *   url:'/productCategory/update/'+id,
     *     method:'post',
     *     data:data
     */
    @ApiOperation("商品分类修改")
    @PostMapping (value = "/update/{id}")
    @ResponseBody
    public CommonResult update(@RequestBody  PmsProductCategoryDTO pmsProductCategoryDTO) {
        boolean result = pmsProductCategoryService.update(pmsProductCategoryDTO);
        if (result) {
            return CommonResult.success(result);
        } else {
            return CommonResult.failed();
        }
    }
    /**
     *  获取商品一级分类和二级分类的下拉级联数据
     *  url:'/productCategory/list/withChildren',
     *  method:'get'
     */
    @GetMapping(value = "/list/withChildren")
    public CommonResult getWithChildren(){
      List<ProductCateChildernDTO> list=  pmsProductCategoryService.getWithChildren();

      return CommonResult.success(list);
    }




}

